/**
 * @file task_misc.h
 * @brief 
 * @author  ()
 * @version 1.0
 * @date 2024-09-13
 * 
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-13 <td>1.0     <td>     <td>内容
 * </table>
 */
#ifndef __TASK_MISC_H
#define __TASK_MISC_H

#ifdef __cplusplus
extern "C" {
#endif

#include "flash_main.h"
#include "key_params/key_params.h"





#ifndef SN_MAX_SIZE
#define SN_MAX_SIZE     (32)
#endif
#ifndef IMEI_MAX_SIZE
#define IMEI_MAX_SIZE   (32)
#endif
#ifndef ICCID_MAX_SIZE
#define ICCID_MAX_SIZE   (32)
#endif
/// @brief 设备参数结构体
typedef struct {
    uint32_t crc;           /* 参数校验码，关机前判断校验更新则保存最新参数 */
    char sn[SN_MAX_SIZE];    
    char iccid[ICCID_MAX_SIZE];
    char imei[IMEI_MAX_SIZE];
    apn_param_t apn;
    uint8_t fota_type;
    char quec_sn[SN_MAX_SIZE];  /* 移为专用SN */
    server_params_t server;     /* 服务器参数 */
    server_ssl_params_t ssl;      /* 服务器CA证书 */
}device_param_t;

/**
 * @brief MCU状态结构体
 */
typedef struct {
    char fota_mark[FOTA_MARK_MAX_SIZE]; /* 升级标志 */
}device_mcu_t;


/**
 * @brief COMCU状态结构体
 */
typedef struct {
    uint8_t reserved; // 用于存放仪表mcu的状态信息
}device_comcu_t;

/**
 * @brief ECU状态结构体
 */
typedef struct {
    uint8_t reserved; // 用于存放电控ecu的状态信息
}device_ecu_t;

/**
 * @brief BMS状态结构体；包含主电池和备用电池
 */
typedef struct {
    uint8_t reserved; // 用于存放bms的状态信息
}device_bms_t;


/**
 * @brief BLE状态结构体
 */
typedef struct {
    uint8_t reserved; // 用于存放ble的状态信息
}device_ble_t;


typedef struct 
{
	float temp;
	float volt;
	float power;   /*percentage of total power*/	
}device_libat_t;


/**
 * @brief 设备状态结构体
 */
typedef struct {
    device_comcu_t comcu;       /*!< co-mcu信息 */
    device_mcu_t mcu;           /*!< iot-mcu信息 */
    device_ecu_t ecu;           /*!< 电控信息 */
    device_bms_t bms;           /*!< BMS信息 */
    device_ble_t ble;           /*!< 蓝牙信息 */
    device_libat_t libat;       /*!< 小电池信息 */

    bool dqa_mode;              /*!< 是否为DQA模式 */
}device_status_t;


/* 运行中存储一次FLASH间隔 */
#define MISC_SAVE_INTERVAL      60
#define MISC_UINT_MAX_VALUE     0XFFFFFFFF

typedef struct {
	unsigned int  save_interval;    /* 存储FLASH间隔 */
    unsigned int  run_tim;
#if 0
	
    unsigned int  pcba_tim;
    unsigned char pcba_ad_cnt;
    unsigned int  pcba_ad_buff[PCBA_AD_MAX_CNT];
    unsigned int  pcba_ad;
    char          pcba_ver[PCBA_VER_LEN];
#if defined(GV300TPCE) || defined(GV300TPCELITE) || defined(GV300TPMG)
    bool          obd_reboot_flag;
#endif
#endif

    uint8_t fota_type;
} misc_info_t;

/* 混杂存储FLASH数据 */
#define MISC_WRITE_FLASH_END    0XA5

typedef struct {
    unsigned char save_flag;
    unsigned char ign_state;
    unsigned char rtc_flag;
    struct tm     rtc;

#if 0	
    struct {
        unsigned char status;
        unsigned short duration;
        unsigned char toggle_times;
		unsigned int  long_time;
    } output[MAX_OUTPUT_NUM];
    struct {
        unsigned int fuel_voltage;
        unsigned int fuel_percent;
    } analog_input[AIS_INPUT_CNT];
    char log_need_save;
    char log_mask_table[20];   //MAX_LOG_ITEMS
    unsigned char jam_status;
    ign_info_t  ign_info;
    unsigned char  volt_percent;    /*电量百分比*/
    unsigned short volt;                 /*电量*/
    unsigned int volt_timestamp;   /* 存储的内电池电压RTC时间 */
    unsigned short crash_event_id;    /* 撞车事件ID */
    

    char pfa_execution_flag;          /* PFA触发执行关机过程标记 */
#else
    char reserved[100];

#endif
} misc_save_t;


extern device_status_t dev_sta;
extern device_param_t dev_prm;

extern misc_save_t misc_save;

int32_t misc_device_init(void);
void misc_param_ram2flash(void);

void misc_flash_data_init(void);
void misc_read_data_from_flash(void);
int misc_write_data_to_flash(void);
void misc_save_data_to_flash(void);

uint8_t misc_read_fota_type(void);
const char *misc_read_quec_sn(void);
void misc_save_fota_type(uint8_t fota_type);

bool lowpwr_prestop_init_subsec(void);
void lowpwr_poststop_exit_subsec(void);

bool lowpwr_prestop_init_subsec(void);
void lowpwr_poststop_exit_subsec(void);

void rtc_wakeup_isr(void);
bool cmd_sleep_status_get(void);
#ifdef __cplusplus
}
#endif

#endif // __TASK_MISC_H